<template>
	<el-main class="bg edit_wrap">
		<el-form ref="form" :model="form" status-icon label-width="120px" v-if="is_view()">
			<el-col v-if="user_group === '管理员' || $check_field('get','order_number') || $check_field('add','order_number') || $check_field('set','order_number')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="订单编号" prop="order_number">
					<el-input id="order_number" v-model="form['order_number']" placeholder="请输入订单编号"
							  v-if="user_group === '管理员' || (form['logistics_information_id'] && $check_field('set','order_number')) || (!form['logistics_information_id'] && $check_field('add','order_number'))" :disabled="disabledObj['order_number_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','order_number')">{{form['order_number']}}</div>
				</el-form-item>
			</el-col>
			<el-col v-if="user_group === '管理员' || $check_field('get','pet_name') || $check_field('add','pet_name') || $check_field('set','pet_name')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="宠物名称" prop="pet_name">
					<el-input id="pet_name" v-model="form['pet_name']" placeholder="请输入宠物名称"
							  v-if="user_group === '管理员' || (form['logistics_information_id'] && $check_field('set','pet_name')) || (!form['logistics_information_id'] && $check_field('add','pet_name'))" :disabled="disabledObj['pet_name_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','pet_name')">{{form['pet_name']}}</div>
				</el-form-item>
			</el-col>
			<el-col v-if="user_group === '管理员' || $check_field('get','pet_type') || $check_field('add','pet_type') || $check_field('set','pet_type')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="宠物类型" prop="pet_type">
					<el-input id="pet_type" v-model="form['pet_type']" placeholder="请输入宠物类型"
							  v-if="user_group === '管理员' || (form['logistics_information_id'] && $check_field('set','pet_type')) || (!form['logistics_information_id'] && $check_field('add','pet_type'))" :disabled="disabledObj['pet_type_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','pet_type')">{{form['pet_type']}}</div>
				</el-form-item>
			</el-col>
			<el-col v-if="user_group === '管理员' || $check_field('get','purchase_user') || $check_field('add','purchase_user') || $check_field('set','purchase_user')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="购买用户" prop="purchase_user">
						<el-select v-if="user_group === '管理员' || (form['logistics_information_id'] && $check_field('set','purchase_user')) || (!form['logistics_information_id'] && $check_field('add','purchase_user'))" id="purchase_user" v-model="form['purchase_user']" :disabled="disabledObj['purchase_user_isDisabled']">
							<el-option v-for="o in list_user_purchase_user" :key="o['username']" :label="o['nickname'] + '-' + o['username']"
									   :value="o['user_id']">
							</el-option>
						</el-select>
						<el-select v-else-if="$check_field('get','purchase_user')" id="purchase_user" v-model="form['purchase_user']" :disabled="true">
							<el-option v-for="o in list_user_purchase_user" :key="o['username']" :label="o['nickname'] + '-' + o['username']"
									   :value="o['user_id']">
							</el-option>
						</el-select>
				</el-form-item>
			</el-col>
			<el-col v-if="user_group === '管理员' || $check_field('get','contact_information') || $check_field('add','contact_information') || $check_field('set','contact_information')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="联系方式" prop="contact_information">
					<el-input id="contact_information" v-model="form['contact_information']" placeholder="请输入联系方式"
							  v-if="user_group === '管理员' || (form['logistics_information_id'] && $check_field('set','contact_information')) || (!form['logistics_information_id'] && $check_field('add','contact_information'))" :disabled="disabledObj['contact_information_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','contact_information')">{{form['contact_information']}}</div>
				</el-form-item>
			</el-col>
			<el-col v-if="user_group === '管理员' || $check_field('get','receiving_address') || $check_field('add','receiving_address') || $check_field('set','receiving_address')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="收货地址" prop="receiving_address">
					<el-input id="receiving_address" v-model="form['receiving_address']" placeholder="请输入收货地址"
							  v-if="user_group === '管理员' || (form['logistics_information_id'] && $check_field('set','receiving_address')) || (!form['logistics_information_id'] && $check_field('add','receiving_address'))" :disabled="disabledObj['receiving_address_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','receiving_address')">{{form['receiving_address']}}</div>
				</el-form-item>
			</el-col>
			<el-col v-if="user_group === '管理员' || $check_field('get','pet_price') || $check_field('add','pet_price') || $check_field('set','pet_price')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="宠物价格" prop="pet_price">
					<el-input id="pet_price" v-model="form['pet_price']" placeholder="请输入宠物价格"
							  v-if="user_group === '管理员' || (form['logistics_information_id'] && $check_field('set','pet_price')) || (!form['logistics_information_id'] && $check_field('add','pet_price'))" :disabled="disabledObj['pet_price_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','pet_price')">{{form['pet_price']}}</div>
				</el-form-item>
			</el-col>
			<el-col v-if="user_group === '管理员' || $check_field('get','purchase_quantity') || $check_field('add','purchase_quantity') || $check_field('set','purchase_quantity')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="购买数量" prop="purchase_quantity">
					<el-input-number id="purchase_quantity" v-model.number="form['purchase_quantity']"
						v-if="user_group === '管理员' || (form['logistics_information_id'] && $check_field('set','purchase_quantity')) || (!form['logistics_information_id'] && $check_field('add','purchase_quantity'))" :disabled="disabledObj['purchase_quantity_isDisabled']"></el-input-number>
					<div v-else-if="$check_field('get','purchase_quantity')">{{form['purchase_quantity']}}</div>
				</el-form-item>
			</el-col>
			<el-col v-if="user_group === '管理员' || $check_field('get','payment_amount') || $check_field('add','payment_amount') || $check_field('set','payment_amount')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="支付金额" prop="payment_amount">
					<el-input id="payment_amount" v-model="form['payment_amount']" placeholder="请输入支付金额"
							  v-if="user_group === '管理员' || (form['logistics_information_id'] && $check_field('set','payment_amount')) || (!form['logistics_information_id'] && $check_field('add','payment_amount'))" :disabled="disabledObj['payment_amount_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','payment_amount')">{{form['payment_amount']}}</div>
				</el-form-item>
			</el-col>
			<el-col v-if="user_group === '管理员' || $check_field('get','logistics_status') || $check_field('add','logistics_status') || $check_field('set','logistics_status')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="物流状态" prop="logistics_status">
					<el-select id="logistics_status" v-model="form['logistics_status']"
						v-if="user_group === '管理员' || (form['logistics_information_id'] && $check_field('set','logistics_status')) || (!form['logistics_information_id'] && $check_field('add','logistics_status'))">
						<el-option v-for="o in list_logistics_status" :key="o" :label="o" :value="o">
						</el-option>
					</el-select>
					<div v-else-if="$check_field('get','logistics_status')">{{form['logistics_status']}}</div>
				</el-form-item>
			</el-col>
			<el-col v-if="user_group === '管理员' || $check_field('get','sign_in_status') || $check_field('add','sign_in_status') || $check_field('set','sign_in_status')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="签收状态" prop="sign_in_status">
					<el-select id="sign_in_status" v-model="form['sign_in_status']"
						v-if="user_group === '管理员' || (form['logistics_information_id'] && $check_field('set','sign_in_status')) || (!form['logistics_information_id'] && $check_field('add','sign_in_status'))">
						<el-option v-for="o in list_sign_in_status" :key="o" :label="o" :value="o">
						</el-option>
					</el-select>
					<div v-else-if="$check_field('get','sign_in_status')">{{form['sign_in_status']}}</div>
				</el-form-item>
			</el-col>
			<el-col :xs="24" :sm="12" :lg="8" class="el_form_btn_warp">
				<el-form-item>
					<el-button type="primary" @click="submit()">提交</el-button>
					<el-button @click="cancel()">取消</el-button>
				</el-form-item>
			</el-col>

		</el-form>
	</el-main>
</template>

<script>
	import mixin from "@/mixins/page.js";

	export default {
		mixins: [mixin],
		data() {
			return {
				field: "logistics_information_id",
				url_add: "~/api/logistics_information/add?",
				url_set: "~/api/logistics_information/set?",
				url_get_obj: "~/api/logistics_information/get_obj?",
				url_upload: "~/api/logistics_information/upload?",

				query: {
					"logistics_information_id": 0,
				},

				form: {
					"order_number":'', // 订单编号
					"pet_name":'', // 宠物名称
					"pet_type":'', // 宠物类型
					"purchase_user": 0, // 购买用户
					"contact_information":'', // 联系方式
					"receiving_address":'', // 收货地址
					"pet_price":'', // 宠物价格
					"purchase_quantity":0, // 购买数量
					"payment_amount":'', // 支付金额
					"logistics_status":'', // 物流状态
					"sign_in_status":'', // 签收状态
					"logistics_information_id": 0, // ID

				},
				disabledObj:{
					"order_number_isDisabled": false,
					"pet_name_isDisabled": false,
					"pet_type_isDisabled": false,
					"purchase_user_isDisabled": false,
					"contact_information_isDisabled": false,
					"receiving_address_isDisabled": false,
					"pet_price_isDisabled": false,
          "purchase_quantity_isDisabled": false,
					"payment_amount_isDisabled": false,
					"logistics_status_isDisabled": false,
					"sign_in_status_isDisabled": false,
				},
				// 用户列表
				list_user_purchase_user: [],
				//物流状态选项列表
				list_logistics_status: ['揽件中','已发货','运输中','待签收'],
				//签收状态选项列表
				list_sign_in_status: ['已签收'],
			}
		},
		methods: {
			/**
			 * 获取普通用户用户列表
			 */
			async get_list_user_purchase_user() {
                // if(this.user_group !== "管理员" && this.form["purchase_user"] === 0) {
                //     this.form["purchase_user"] = this.user.user_id;
                // }
                var json = await this.$get("~/api/user/get_list?user_group=普通用户");
                if(json.result && json.result.list){
                    this.list_user_purchase_user = json.result.list;
                }
                else if(json.error){
                    console.error(json.error);
                }
			},
			get_user_purchase_user(id){
				var obj = this.list_user_purchase_user.getObj({"user_id":id});
				var ret = "";
				if(obj){
					if(obj.nickname){
						ret = obj.nickname;}
					else{
						ret = obj.username;
					}
				}
				return ret;
			},

			/**
			 * 获取对象之前
			 * @param {Object} param
			 */
			get_obj_before(param) {
				var form = "";
				/**
				* 请求列表前
				* @param {Object} param
				*/
				var user_group = this.user.user_group;
				if (user_group !== "管理员") {
					switch (user_group) {
						case "购买用户":
							if(param["purchase_user"] > 0){
								param["purchase_user"] = this.user.user_id;
							}
							break;
					}
				}
				if(this.form && form){
					Object.keys(this.form).forEach(key => {
						Object.keys(form).forEach(dbKey => {
							// if(dbKey === "charging_standard"){
							// 	this.form['charging_rules'] = form[dbKey];
							// 	this.disabledObj['charging_rules_isDisabled'] = true;
							// };
							if(key === dbKey){
								this.disabledObj[key+'_isDisabled'] = true;
							}
						})
					})
				}
				$.db.del("form");
				return param;
			},

			/**
			 * 获取对象之后
			 * @param {Object} json
			 * @param {Object} func
			 */
			get_obj_after(json, func){
			},

			is_view(){
				var bl = this.user_group == "管理员";

				if(!bl){
					bl = this.$check_action('/logistics_information/table','add');
					console.log(bl ? "你有表格添加权限视作有添加权限" : "你没有表格添加权限");
				}
				if(!bl){
					bl = this.$check_action('/logistics_information/table','set');
					console.log(bl ? "你有表格添加权限视作有修改权限" : "你没有表格修改权限");
				}
				if(!bl){
					bl = this.$check_action('/logistics_information/view','add');
					console.log(bl ? "你有视图添加权限视作有添加权限" : "你没有视图添加权限");
				}
				if(!bl){
					bl = this.$check_action('/logistics_information/view','set');
					console.log(bl ? "你有视图修改权限视作有修改权限" : "你没有视图修改权限");
				}
				if(!bl){
					bl = this.$check_action('/logistics_information/view','get');
					console.log(bl ? "你有视图查询权限视作有查询权限" : "你没有视图查询权限");
				}

				console.log(bl ? "具有当前页面的查看权，请注意这不代表你有字段的查看权" : "无权查看当前页，请注意即便有字段查询权限没有页面查询权限也不行");

				return bl;
			},
			/**
			 * 上传文件
			 * @param {Object} param
			 */
			uploadimg(param) {
				this.uploadFile(param.file, "avatar");
			},

		},
		created() {
			this.get_list_user_purchase_user();
		},
	}
</script>

<style>
	.avatar-uploader .el-upload {
		border: 1px dashed #d9d9d9;
		border-radius: 6px;
		cursor: pointer;
		position: relative;
		overflow: hidden;
	}

	.avatar-uploader .el-upload:hover {
		border-color: #409EFF;
	}

	.avatar-uploader-icon {
		font-size: 28px;
		color: #8c939d;
		width: 178px;
		height: 178px;
		line-height: 178px;
		text-align: center;
	}

	.avatar {
		width: 178px;
		height: 178px;
		display: block;
	}
</style>
